//
//  HotNewsPageVC.m
//  phmall
//
//  Created by admin on 2017/12/8.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "HotNewsPageVC.h"
#import "SDCursorView.h"
#import "HotNewsViewController.h"
#import "ClassifyView.h"
#import "GoodsClassifyVC.h"
@interface HotNewsPageVC ()<ClassifyViewDelegate,SDCursorViewDelegate>{
    ClassifyView *classView;
    UIButton *more;
}
@property (nonatomic, strong) SDCursorView *cursorView;
@end

@implementation HotNewsPageVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"1.0";
    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor =  [UIColor whiteColor];
}

-(void)rightBarAction{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=Localized(@"资讯热点");
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    UIBarButtonItem* rightBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜索"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarAction)];
    self.navigationItem.rightBarButtonItem=rightBar;
    
    YYCache *cache = [YYCache cacheWithName:@"category"];
    if ([cache containsObjectForKey:@"name"]) {
        NSMutableArray *category = (NSMutableArray *)[cache objectForKey:@"name"];
        NSLog(@"%@",category);
        [self reloadPages:category];
    }
    // Do any additional setup after loading the view.
}
-(void)didSelectItemAtIndexPath:(NSString *)brand{
    
    
}
-(void)reloadPages:(NSMutableArray *)title{
    NSMutableArray *classtitle=[[NSMutableArray alloc]init];
    for (int i=0; i<title.count; i++) {
        NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"];
        if ([language hasPrefix:@"en"]) {
            
            [classtitle addObject:title[i][@"category_name"]];
        } else if ([language hasPrefix:@"zh"]) {
            
            [classtitle addObject:title[i][@"category_name_ch"]];
        }
    }
    _cursorView = [[SDCursorView alloc]init];
    [self.view addSubview:_cursorView];
    [_cursorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(44);
    }];
    [_cursorView.superview layoutIfNeeded];
    //设置子页面容器的高度
    _cursorView.contentViewHeight = self.view.bounds.size.height-44-(STATUS_BAR_HEIGHT+44)-self.tabBar_height;
    _cursorView.delegate=self;
    //设置控件所在controller
    _cursorView.parentViewController = self;
    _cursorView.titles =classtitle;
    
    //    _cursorView.titles=titles;
    //设置所有子controller
    NSMutableArray *contrors = [NSMutableArray array];
    _cursorView.currentIndex=0;
    
    for (int i=0; i<classtitle.count; i++) {
        HotNewsViewController *controller = [[HotNewsViewController alloc]init];
        //        controller.uid=title[i][@"uid"];
        [contrors addObject:controller];
    }
    _cursorView.controllers = [contrors copy];
    //设置字体和颜色
    _cursorView.normalColor = [UIColor blackColor];
    _cursorView.selectedColor = [UIColor colorWithHexString:@"#ff8000"];
    _cursorView.selectedFont = [UIFont systemFontOfSize:13];
    _cursorView.normalFont = [UIFont systemFontOfSize:13];
    
    //    _cursorView.lineView.backgroundColor = [UIColor redColor];
    //属性设置完成后，调用此方法绘制界面
    [_cursorView reloadPages];
    
    more=[UIButton buttonWithType:UIButtonTypeCustom];
    [more setImage:[UIImage imageNamed:@"下拉展开"] forState:UIControlStateNormal];
    [more setImage:[UIImage imageNamed:@"上拉收起"] forState:UIControlStateSelected];
    more.backgroundColor = [UIColor colorWithHexString:@"#f8fafd"];
    [more addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:more];
    [more mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_cursorView).offset(0);
        make.centerY.equalTo(_cursorView);
        make.width.height.offset(44);
    }];
    
    
    //    UIButton *screen = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [self.view addSubview:screen];
    //    [screen addTarget:self action:@selector(screenAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [screen setBackgroundImage:[UIImage imageNamed:@"置顶-底"] forState:UIControlStateNormal];
    //    [screen setImage:[UIImage imageNamed:@"置顶"] forState:UIControlStateNormal];
    //    [screen mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.equalTo(self.view).with.offset(20);
    //        make.bottom.equalTo(self.view).with.offset(-59);
    //        make.width.height.offset(50);
    //    }];
    
    classView=[[ClassifyView alloc]init];
    classView.delegate=self;
    classView.hidden=YES;
    classView.titleArray=classtitle;
    [self.view addSubview:classView];
    classView.layer.masksToBounds=YES;
    [classView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_cursorView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    //    sequencing=[[SequencingView alloc]init];
    //    sequencing.delegate=self;
    //    UIApplication *ap = [UIApplication sharedApplication];
    //    [ap.keyWindow addSubview:sequencing];
    //    sequencing.hidden=YES;
    //    //    [self.view addSubview:sequencing];
    //    [sequencing mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(ap.keyWindow).offset(0);
    //        make.left.right.equalTo(ap.keyWindow);
    //        make.bottom.equalTo(ap.keyWindow);
    //    }];
}

- (void)moreAction:(UIButton *)sender{
    more.selected=!more.selected;
    if (more.selected) {
        classView.hidden=NO;
    }else{
        classView.hidden=YES;
    }
}
-(void)withdrawClassifyView:(NSInteger)index{
    _cursorView.currentIndex=index;
    more.selected=!more.selected;
    classView.hidden=YES;
    //属性设置完成后，调用此方法绘制界面
    [_cursorView reloadPages];
}
-(void)setSelectAtIndex:(NSInteger)index{
    more.selected=NO;
    classView.hidden=YES;
    [classView setSelectAtIndex:index];
}
-(void)withdrawClassifyView{
    more.selected=!more.selected;
    classView.hidden=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
